package com.example.pc.updata.DaTi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pc.http.API;
import com.example.pc.http.JS_Utils;
import com.example.pc.http.Okhttp;
import com.example.pc.updata.*;
import com.example.pc.updata.Baes.BaseActivity;

import com.example.pc.updata.obj.Ts;
import com.example.pc.utils.JSONUtils;
import com.example.pc.utils.SpfUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class DaTiActivity extends BaseActivity {

    List<Ts.DataBean> list_timu = new ArrayList<>();
    @BindView(R.id.tv1_num)
    TextView tv1Num;

    @BindView(R.id.tv1_dangqian)
    TextView tv1_dangqian;


    @BindView(R.id.tv1_name)
    TextView tv1Name;


    @BindView(R.id.radio_one)
    RadioButton radioOne;
    @BindView(R.id.radio_two)
    RadioButton radioTwo;
    @BindView(R.id.radio_three)
    RadioButton radioThree;
    @BindView(R.id.radio_four)
    RadioButton radioFour;
    @BindView(R.id.radioGroup_gender)
    RadioGroup radioGroupGender;
    @BindView(R.id.btn_sub)
    Button btnSub;
    @BindView(R.id.btn_shang)
    Button btn_shang;
    @BindView(R.id.btn_fk2)
    Button btn_fk2;


    Ts.DataBean tiMu;
    int num = 0;

    int duiNum = 0;
    String user_uid;

    /**
     * 这是兼容的AlertDialog
     */
    private void showDialog(String ST) {
  /*
   * 这里使用了 android.support.v7.app.AlertDialog.Builder
   * 可以直接在头部写 import android.support.v7.app.AlertDialog
   * 那么下面就可以写成 AlertDialog.Builder
   */
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage(ST);
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", null);
        builder.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_da_ti);
        ButterKnife.bind(this);
        num = 0;
        user_uid = (String) SpfUtils.get(DaTiActivity.this, "user_uid", "");
//        findAll();
//        btn_fk.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                tiMu = list_timu.get(num);
//                showDialog(tiMu.getJx());
//
//            }
//        });

        btn_fk2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DaTiActivity.this, FanKuiActivity.class);
                intent.putExtra("num1", list_timu.size());
                intent.putExtra("num2", num);
                startActivity(intent);

            }
        });
        tv1_dangqian.setText("当前第" + num + "/15道");
        tv1Num.setText("正确数:" + duiNum + "道");
        btnSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (num < list_timu.size() - 1) {
                    btnSub.setText("下一步");
                    btn_shang.setText("上一步");
                    for (int i = 0; i < radioGroupGender.getChildCount(); i++) {
                        RadioButton childAt = (RadioButton) radioGroupGender.getChildAt(i);

                        int dd = radioGroupGender.getCheckedRadioButtonId();
//                        dd != -1
                        if (childAt.isChecked()) {

                            String TYPE = "A";
                            if (i == 0) {
                                TYPE = "A";
                            } else if (i == 1) {
                                TYPE = "B";
                            } else if (i == 2) {
                                TYPE = "C";
                            } else {
                                TYPE = "D";
                            }

                            if (TYPE.equals(tiMu.getZq())) {
                                duiNum = duiNum + 1;
                                tv1Num.setText("正确数:" + duiNum + "道");
                            } else {
                                Log.e("1111", "答错");

                                addcuo(tiMu.getTm(),

                                        tiMu.getZq(),
                                        TYPE,
                                        tiMu.getJx(),
                                        user_uid);

                            }


                        } else {
                            Log.e("1111", "答错2");
//                            addcuo(tiMu.getTm(),
//
//                                    tiMu.getZq(),
//                                    "A",
//                                    tiMu.getJx(),
//                                    user_uid);

                        }
//                        else {
//                            Toast.makeText(DaTi2Activity.this, "请答题", Toast.LENGTH_SHORT).show();
//                            return;
//                        }

                    }

                    num = num + 1;
                    tv1_dangqian.setText("当前第" + num + "/15道");

                    Log.e("1", "xx" + num);
                    tiMu = list_timu.get(num);
                    tv1Name.setText(tiMu.getTm());
                    radioOne.setText("A:" + tiMu.getA());
                    radioTwo.setText("B:" + tiMu.getB());
                    radioThree.setText("C:" + tiMu.getC());
                    radioFour.setText("D:" + tiMu.getD());
                    for (int z = 0; z < radioGroupGender.getChildCount(); z++) {
                        RadioButton childAt2 = (RadioButton) radioGroupGender.getChildAt(z);
                        childAt2.setChecked(false);
                    }
                } else

                {
                    btnSub.setText("交卷");
                    startActivity(new Intent(DaTiActivity.this, ZongJieActivity.class).putExtra("dati", "" + duiNum));
//                    Toast.makeText(DaTi2Activity.this, "恭喜，答对" + duiNum + "道题", Toast.LENGTH_SHORT).show();
                    finish();

                }
            }
        });

        btn_shang.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                if (num <= 0) {
                    Toast.makeText(DaTiActivity.this, "暂无上一题", Toast.LENGTH_SHORT).show();
                    return;

                }
                if (num < list_timu.size() - 1) {
                    btn_shang.setText("上一步");
                    for (int i = 0; i < radioGroupGender.getChildCount(); i++) {
                        RadioButton childAt = (RadioButton) radioGroupGender.getChildAt(i);
                        if (childAt.isChecked()) {
                            String TYPE = "A";
                            if (i == 0) {
                                TYPE = "A";
                            } else if (i == 1) {
                                TYPE = "B";
                            } else if (i == 2) {
                                TYPE = "C";
                            } else {
                                TYPE = "D";
                            }

//                            if (TYPE.equals(tiMu.getZq())) {
//                                duiNum = duiNum + 1;
//                                tv1Num.setText("正确数:" + duiNum + "道");
//                            }
//                            }else {
//                                tv1Num.setText("当前第"+num+"/15道，"+"正确数:" + duiNum+"道");
//                            }

                        }

                    }

                    num = num - 1;
                    tv1_dangqian.setText("当前第" + num + "/15道");

                    Log.e("1", "xx" + num);
                    if (num <= 0) {
                        return;
                    }
                    tiMu = list_timu.get(num);
                    tv1Name.setText(tiMu.getTm());
                    radioOne.setText("A:" + tiMu.getA());
                    radioTwo.setText("B:" + tiMu.getB());
                    radioThree.setText("C:" + tiMu.getC());
                    radioFour.setText("D:" + tiMu.getD());
                    for (int i = 0; i < radioGroupGender.getChildCount(); i++) {
                        RadioButton childAt = (RadioButton) radioGroupGender.getChildAt(i);
                        childAt.setChecked(false);
                    }


                } else {
                    btnSub.setText("暂无上一题");
//                    startActivity(new Intent(DaTi2Activity.this, ZongJieActivity.class).putExtra("dati", "" + duiNum));
//                    Toast.makeText(DaTi2Activity.this, "恭喜，答对" + duiNum + "道题", Toast.LENGTH_SHORT).show();


                }
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        num = 0;
        duiNum = 0;

        findAll();
    }


    private void findAll() {
        API.gettimus(new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Ts ts = JSONUtils.parseJSON(st, Ts.class);
                List<Ts.DataBean> list = ts.getData();

                if (!list.isEmpty() && list.size() > 0) {

                    list_timu = JS_Utils.getSubStringByRadom(list, 15);
                    if (list_timu.isEmpty() && list_timu.size() == 0) {
                        showMessage("请添加目管理");
                        tv1Num.setVisibility(View.GONE);
                        tv1Name.setVisibility(View.GONE);
                        radioGroupGender.setVisibility(View.GONE);
                        btnSub.setVisibility(View.GONE);
                    } else {
                        if (list_timu != null && list_timu.size() > 0) {
                            tiMu = list_timu.get(num);
                            tv1Name.setText(tiMu.getTm());
                            radioOne.setText("A:" + tiMu.getA());
                            radioTwo.setText("B:" + tiMu.getB());
                            radioThree.setText("C:" + tiMu.getC());
                            radioFour.setText("D:" + tiMu.getD());
                            for (int i = 0; i < radioGroupGender.getChildCount(); i++) {
                                RadioButton childAt = (RadioButton) radioGroupGender.getChildAt(i);
                                childAt.setChecked(false);
                            }

                        }
                    }
                }
            }

            @Override
            public void onFalia(int code, String errst) {

            }
        });

    }

    public void addcuo(String tm,
                       String zq,
                       String cw,
                       String jx,
                       String user_uid) {
        API.addctimu(tm, zq, cw, jx, user_uid, new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("111", "xx" + st);

            }

            @Override
            public void onFalia(int code, String errst) {
                Log.e("111", "xx" + errst);
            }
        });

    }
}
